草庐IT

Java final 与 C++ const

全部标签

c++ - const_cast 未定义行为的重要示例

据我了解,以下代码是根据c++标准(特别是第7.1.5.1.4[dcl.type.cv]/4节)未定义的行为。#includestructF;F*g;structF{F():val(5){g=this;}intval;};constFf;intmain(){g->val=8;std::cout但是,我尝试过的每个编译器和优化设置都会打印“8”。问题:这种类型的“隐式const_cast”是否有会出现意外结果的示例?我希望有一些像结果一样壮观的东西#includeintmain(){for(inti=0;i开启,例如,带有-O2的gcc4.8.5EDIT:标准中的相关部分7.1.5.1.

c++ - const_cast 未定义行为的重要示例

据我了解,以下代码是根据c++标准(特别是第7.1.5.1.4[dcl.type.cv]/4节)未定义的行为。#includestructF;F*g;structF{F():val(5){g=this;}intval;};constFf;intmain(){g->val=8;std::cout但是,我尝试过的每个编译器和优化设置都会打印“8”。问题:这种类型的“隐式const_cast”是否有会出现意外结果的示例?我希望有一些像结果一样壮观的东西#includeintmain(){for(inti=0;i开启,例如,带有-O2的gcc4.8.5EDIT:标准中的相关部分7.1.5.1.

c++ - 为什么 "auto const&"不是只读的?

这个问题在这里已经有了答案:Variablesmarkedasconstusingstructuredbindingsarenotconst(1个回答)关闭4年前.#includeintmain(){intxa=1;intya=2;autoconst&[xb,yb]=std::tuple(xa,ya);xb=9;//Shouldn'tthisberead-only?returnxa+ya;}这不仅编译,而且返回11。那么两个问题:为什么当xb被指定为autoconst&时我可以写入?这不应该编译失败吗?为什么我不能用“auto&”替换“autoconst&”并让它编译?Clang(6.

c++ - 为什么 "auto const&"不是只读的?

这个问题在这里已经有了答案:Variablesmarkedasconstusingstructuredbindingsarenotconst(1个回答)关闭4年前.#includeintmain(){intxa=1;intya=2;autoconst&[xb,yb]=std::tuple(xa,ya);xb=9;//Shouldn'tthisberead-only?returnxa+ya;}这不仅编译,而且返回11。那么两个问题:为什么当xb被指定为autoconst&时我可以写入?这不应该编译失败吗?为什么我不能用“auto&”替换“autoconst&”并让它编译?Clang(6.

c++ - `unique_ptr< T const [] >` 是否应该接受 `T*` 构造函数参数?

代码:#includeusingnamespacestd;structT{};T*foo(){returnnewT;}Tconst*bar(){returnfoo();}intmain(){unique_ptrp1(bar());//OKunique_ptra1(bar());//OKunique_ptrp2(foo());//OKunique_ptra2(foo());//?thisisline#15}VisualC++10.0和MinGWg++4.4.1的示例错误:[d:\dev\test]>clfoo.cppfoo.cppfoo.cpp(15):errorC2248:'std::

c++ - `unique_ptr< T const [] >` 是否应该接受 `T*` 构造函数参数?

代码:#includeusingnamespacestd;structT{};T*foo(){returnnewT;}Tconst*bar(){returnfoo();}intmain(){unique_ptrp1(bar());//OKunique_ptra1(bar());//OKunique_ptrp2(foo());//OKunique_ptra2(foo());//?thisisline#15}VisualC++10.0和MinGWg++4.4.1的示例错误:[d:\dev\test]>clfoo.cppfoo.cppfoo.cpp(15):errorC2248:'std::

c++ - const对象的构造

C++11§12.1/14:Duringtheconstructionofaconstobject,ifthevalueoftheobjectoranyofitssubobjectsisaccessedthroughanlvaluethatisnotobtained,directlyorindirectly,fromtheconstructor’sthispointer,thevalueoftheobjectorsubobjectthusobtainedisunspecified.[Example:structC;voidno_opt(C*);structC{intc;C():c(0)

c++ - const对象的构造

C++11§12.1/14:Duringtheconstructionofaconstobject,ifthevalueoftheobjectoranyofitssubobjectsisaccessedthroughanlvaluethatisnotobtained,directlyorindirectly,fromtheconstructor’sthispointer,thevalueoftheobjectorsubobjectthusobtainedisunspecified.[Example:structC;voidno_opt(C*);structC{intc;C():c(0)

c++ - 视觉 C++ 2010 : Why "signed/unsigned mismatch" disappears if i add "const" to one comparand?

我有以下简单的C++代码:#include"stdafx.h"intmain(){inta=-10;unsignedintb=10;//Trivialerrorisplacedhereonpurposetotriggerawarning.if(a使用VisualStudio2010(默认C++控制台应用程序)编译,它给出warningC4018:'如预期的那样(代码有逻辑错误)。但如果我改变unsignedintb=10;进入constunsignedintb=10;警告消失!这种行为有什么已知的原因吗?gcc无论const如何,都会显示警告.更新我可以从评论中看到很多人建议“它只是以

c++ - 视觉 C++ 2010 : Why "signed/unsigned mismatch" disappears if i add "const" to one comparand?

我有以下简单的C++代码:#include"stdafx.h"intmain(){inta=-10;unsignedintb=10;//Trivialerrorisplacedhereonpurposetotriggerawarning.if(a使用VisualStudio2010(默认C++控制台应用程序)编译,它给出warningC4018:'如预期的那样(代码有逻辑错误)。但如果我改变unsignedintb=10;进入constunsignedintb=10;警告消失!这种行为有什么已知的原因吗?gcc无论const如何,都会显示警告.更新我可以从评论中看到很多人建议“它只是以